package com.eclipse.e0112_多线程.考试2;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;

/*
 * 写2个线程 一个读取数据到list 另一个打印数据到控制台 用协作或者同步
 */
public class a读取到list_b打印 {
	public static void main(String[] args) {
		Rq rq = new Rq();
		// 打印先运行
		Print print = new Print(rq);
		print.start();
		Read read = new Read(rq);
		read.start();
	}
}

//读取到list
class Read extends Thread {

	List<String> list;
	Rq rq;

	Read(Rq rq) {
		this.rq = rq;
	}

	public void run() {
		synchronized (rq) {
			try {
				list = Files.readAllLines(Paths.get("/Users/liuzhuangzhuang/test/b.txt"));
				rq.setList(list);
			} catch (IOException e) {
				e.printStackTrace();
			}
			System.out.println("读取到list完毕");
			rq.notify();
			System.out.println("唤醒完毕");

		}

	}

}

//打印到控制台
class Print extends Thread {
	Rq rq;
	List<String> list;

	Print(Rq rq) {
		this.rq = rq;
	}

	public void run() {

		synchronized (rq) {
			try {
				// 等待
				rq.wait();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			list = rq.getList();
			for (String temp : list) {
				System.out.println(temp);
			}
		}

	}
}

//中间商
class Rq {
	private List<String> list;

	public List<String> getList() {
		return list;
	}

	public void setList(List<String> list) {
		this.list = list;
	}
}